package com.td.module.system.controller.app.growth;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.system.controller.admin.growth.vo.list.*;
import com.td.module.system.convert.growth.GrowthModuleListConvert;
import com.td.module.system.dal.dataobject.growth.GrowthModuleListDO;
import com.td.module.system.service.growth.GrowthModuleListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户APP - 生长模型列")
@RestController
@RequestMapping("/growth/module-list")
@Validated
public class AppGrowthModuleListController {

    @Resource
    private GrowthModuleListService moduleListService;

    @PostMapping("/create")
    @ApiOperation("创建生长模型列")
    public CommonResult<Long> createModuleList(@Valid @RequestBody GrowthModuleListCreateReqVO createReqVO) {
        return success(moduleListService.createModuleList(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新生长模型列")
    public CommonResult<Boolean> updateModuleList(@Valid @RequestBody GrowthModuleListUpdateReqVO updateReqVO) {
        moduleListService.updateModuleList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除生长模型列")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteModuleList(@RequestParam("id") Long id) {
        moduleListService.deleteModuleList(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得生长模型列")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<GrowthModuleListRespVO> getModuleList(@RequestParam("id") Long id) {
        GrowthModuleListDO moduleList = moduleListService.getModuleList(id);
        return success(GrowthModuleListConvert.INSTANCE.convert(moduleList));
    }

    @GetMapping("/list")
    @ApiOperation("获得生长模型列列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<GrowthModuleListRespVO>> getModuleListList(@RequestParam("ids") Collection<Long> ids) {
        List<GrowthModuleListDO> list = moduleListService.getModuleListList(ids);
        return success(GrowthModuleListConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得生长模型列分页")
    public CommonResult<PageResult<GrowthModuleListRespVO>> getModuleListPage(@Valid GrowthModuleListPageReqVO pageVO) {
        PageResult<GrowthModuleListDO> pageResult = moduleListService.getModuleListPage(pageVO);
        return success(GrowthModuleListConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出生长模型列 Excel")
    @OperateLog(type = EXPORT)
    public void exportModuleListExcel(@Valid GrowthModuleListExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GrowthModuleListDO> list = moduleListService.getModuleListList(exportReqVO);
        // 导出 Excel
        List<GrowthModuleListExcelVO> datas = GrowthModuleListConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生长模型列.xls", "数据", GrowthModuleListExcelVO.class, datas);
    }

}
